/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-web-praise nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.lpitools.gwtgui.server.util;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import java.util.logging.Logger;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sri.ai.lpitools.cobra.gwtutil.dataobjects.shared.TreeNode;
import com.sri.ai.lpitools.cobra.gwtutil.dataobjects.shared.TreeNode.TreeType;

public class TreeNodeCollection {

	private HashMap<String, HashMap<String, ArrayList<TreeNode>>> justificationNodeCollections = new HashMap<String, HashMap<String, ArrayList<TreeNode>>>();
	private HashMap<String, HashMap<String, ArrayList<TreeNode>>> traceNodeCollections = new HashMap<String, HashMap<String, ArrayList<TreeNode>>>();

	private final Logger logger = Logger
			.getLogger(TreeNodeCollection.class.getName());

	public void addSerializedNodes(String clientID,
			List<String> serializedNodes) {
		synchronized (logger) {
			if (!justificationNodeCollections.containsKey(clientID)) {
				justificationNodeCollections.put(clientID,
						new HashMap<String, ArrayList<TreeNode>>());
			}
			HashMap<String, ArrayList<TreeNode>> justificationClientNodes = justificationNodeCollections
					.get(clientID);
			if (!traceNodeCollections.containsKey(clientID)) {
				traceNodeCollections.put(clientID,
						new HashMap<String, ArrayList<TreeNode>>());
			}
			HashMap<String, ArrayList<TreeNode>> traceClientNodes = traceNodeCollections
					.get(clientID);
			for (String s : serializedNodes) {
				Type nodeType = new TypeToken<TreeNode>() {
				}.getType();
				TreeNode newNode;
				try {
					newNode = (new Gson()).fromJson(s, nodeType);
				} catch (Exception ex) {
					logger.info("Could not parse node, not adding node. Serialized node was: "
							+ s);
					return;
				}
				TreeType type = newNode.getType();
				String nodeID = newNode.getID();
				if (!nodeID.contains(".")) {
					logger.info("Invalid node ID found, not adding node. Node ID was: "
							+ nodeID);
					return;
				}

				String parentID = nodeID.substring(0, nodeID.lastIndexOf('.'));
				if (type == TreeType.JUSTIFICATION) {
					if (!justificationClientNodes.containsKey(parentID)) {
						justificationClientNodes.put(parentID,
								new ArrayList<TreeNode>());
					}
					ArrayList<TreeNode> parentCollection = justificationClientNodes
							.get(parentID);
					parentCollection.add(newNode);
				} else if (type == TreeType.TRACE) {
					if (!traceClientNodes.containsKey(parentID)) {
						traceClientNodes.put(parentID,
								new ArrayList<TreeNode>());
					}
					ArrayList<TreeNode> parentCollection = traceClientNodes
							.get(parentID);
					parentCollection.add(newNode);
				} else {
					logger.info("Unrecognized node type, not adding node. Node type was: "
							+ type.toString());
					return;
				}
			}
		}
	}

	public ArrayList<TreeNode> getNodesForParent(String clientID,
			String parentID, TreeNode.TreeType nodeType) {
		synchronized (logger) {
			if (nodeType == TreeType.JUSTIFICATION) {
				if (justificationNodeCollections.get(clientID) == null) {
					return new ArrayList<TreeNode>();
				}
				ArrayList<TreeNode> result = justificationNodeCollections.get(
						clientID).get(parentID);
				if (result == null) {
					return new ArrayList<TreeNode>();
				} else {
					return result;
				}

			} else if (nodeType == TreeType.TRACE) {
				if (traceNodeCollections.get(clientID) == null) {
					return new ArrayList<TreeNode>();
				}
				ArrayList<TreeNode> result = traceNodeCollections.get(
						clientID).get(parentID);
				if (result == null) {
					return new ArrayList<TreeNode>();
				} else {
					return result;
				}
			} else {
				logger.info("Unrecognized node type, not adding node. Node type was: "
						+ nodeType.toString());
				return new ArrayList<TreeNode>();
			}
		}
	}

	public void deleteNodeSet(String clientID) {
		synchronized (logger) {
			justificationNodeCollections.remove(clientID);
			traceNodeCollections.remove(clientID);
		}
	}
}
